package com.example.controller;

import com.example.pojo.Result;
import com.example.pojo.clerkNamePojo;
import com.example.service.clerkNameService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class clerkNameController {
    @Autowired
    private clerkNameService clerkNameService;
    @Value("${file.upload-dir}")
    private String saveDir;
    //psot查询所有；此处与前端一致
    @PostMapping("/queryClerkNameClerkName")
    public Result queryClerkNameClerkName(){
        //查询所有的方法
        List<clerkNamePojo> clerkNamePojo=clerkNameService.queryAll();
        System.out.println("查询成功");
        return Result.success(clerkNamePojo);
    }
    @PostMapping("/addClerkNameClerkName")
    public  Result addClerkNameClerkName(@RequestBody clerkNamePojo clerkNamePojo){
        System.out.println(clerkNamePojo);
        clerkNameService.insertOne(clerkNamePojo);
        System.out.println("添加成功");
        return Result.success();
    }
    @PostMapping("/editClerkNameClerkName")
    public Result editClerkNameClerkName(@RequestBody clerkNamePojo clerkNamePojo){
        clerkNameService.editClerkNameClerkName(clerkNamePojo);
        System.out.println("修改成功");
        return Result.success("修改成功");
    }

   //插入带文件的
    @PostMapping("/addClerkNameClerkNameFile")
    public Result addClerkNameClerkNameFile(
            @RequestParam(value = "file",required = false) MultipartFile file,
            @RequestParam(value = "fileName",required = false) String fileName,
            @RequestParam(value = "type",required = false) int type,
            @RequestParam(value = "oldFileName",required = false) String oldFileName){
        //插入文件
        //
        if(type==0){
            //把文件插入
            try {
                if (!file.isEmpty()) {
                    String filePath = saveDir + fileName;
                    File dest = new File(filePath);
                    // 将文件保存到服务器上的指定位置
                    file.transferTo(dest);
                    return Result.success("图片插入成功");
                }
                else {
                    return Result.success("没有收到文件，请检查");
                }
            }
            catch (IOException e) {
                return Result.success("文件保存出错，请联系管理员");
            }

        }
        return Result.success();
    }





}
